package org.imraniqbal.frogger;

import org.imraniqbal.engine.Direction;
import org.imraniqbal.engine.Game;
import org.imraniqbal.engine.GameEvent;
import org.imraniqbal.engine.GameFrame;
import org.imraniqbal.engine.GameController.Controls;
import org.imraniqbal.engine.Map;

/**
 * @author  Colin MacDougall
 */
public class FroggerGame extends Game {
	
	/**
	 * @uml.property  name="frogger"
	 * @uml.associationEnd  
	 */
	private Frogger frogger;

	/**
	 * @uml.property  name="cars"
	 * @uml.associationEnd  multiplicity="(0 -1)"
	 */
	private Car[] cars;
	/**
	 * @uml.property  name="flowerpatch"
	 * @uml.associationEnd  
	 */
	private Flower flowerpatch;
	/**
	 * @uml.property  name="home"
	 * @uml.associationEnd  
	 */
	private Home home;
	public final static int NumCars = 16; //the number of cars you'll be facing

	public FroggerGame() {
		super();
		start();
	}
	
	@Override
	public void start() {
		this.setMap(new Map("/maps/frogger/FrogMap.txt"));
		this.setWin(false);
		
		home = new Home(9,9);
		getMap().addMappable(home);
		
		flowerpatch = new Flower(9,1);
		getMap().addMappable(flowerpatch);
		
		frogger = new Frogger(9,9,Direction.UP,3); //add frogger
		frogger.spawn(getMap());
		addListener(frogger);
		
		cars = new Car[NumCars];
		for(int i = 0; i < 4; i++){
			cars[i] = new Car(1+(i*3),3,Direction.LEFT,1,2);
			cars[i+4] = new Car(1+(i*3) ,7,Direction.RIGHT,1,1);
			cars[i+8] = new Car(i+8, 4, Direction.RIGHT, 1, i+12);
			cars[i+12] = new Car(i+12, 6, Direction.LEFT,1, i+8);
			getMap().addMappable(cars[i]);
			getMap().addMappable(cars[i+4]);
			getMap().addMappable(cars[i+8]);
			getMap().addMappable(cars[i+12]);
		}

	}

	@Override
	public boolean hasEnded() {
		return (getWin() || getFrogger().getNumOflives() == 0) ;
	}

	/**
	 * @return
	 * @uml.property  name="frogger"
	 */
	public Frogger getFrogger() {
		return frogger;
	}
	@Override
	public void getControl(Controls control) {
		if(hasEnded()){	return;	}
		switch(control){
			case DOWN:
				frogger.setDirection(Direction.DOWN);
				break;
			case LEFT:
				frogger.setDirection(Direction.LEFT);
				break;
			case RIGHT:
				frogger.setDirection(Direction.RIGHT);
				break;
			case UP:
				frogger.setDirection(Direction.UP);
				break;
			case ACTION:
				return;
		}
		frogger.updateLocation(getMap());
		for( Car c : cars){
			c.moveCar(getMap());
		}
		notify(new GameEvent(this));
	}
	
	public static void main(String args[]) {
		FroggerGame tempGame = new FroggerGame(); //create the game
		new GameFrame("Frogger",new FroggerPanel(tempGame)); //set up the frame
	}
}
